package com.zs.toolbox.web2j.common.web;


import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.zs.toolbox.web2j.common.exception.WtExceptions;
import com.zs.toolbox.web2j.common.exception.WtResponses;
import com.zs.toolbox.web2j.common.request.WtPageBodyModel;
import com.zs.toolbox.web2j.common.request.WtPageQuerySearchModel;
import com.zs.toolbox.web2j.common.request.WtPrimaryKeyReqeust;
import com.zs.toolbox.web2j.common.response.WtBaseResponse;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.Serializable;
import java.lang.reflect.Field;

/**
 * <p>
 * 用户基本信息操作
 * </p>
 *
 * @author eric.zhang
 * @since 2018-09-21
 */
//@RequestMapping("/api/v1/admin/user/")
//@Api(description = "用户信息/权限/角色管理")
@Slf4j
public class WtAdminBaseController<M extends IService<T>, T, U extends Serializable> extends WtBaseController {
    @Autowired
    protected M baseService;

    protected String getTableField(Class<T> t, String fieldName) {
        try {
            if (StringUtils.isBlank(fieldName)) {
                return "";
            }
            Field declaredField = FieldUtils.getDeclaredField(t, fieldName,true);
            String tableFiled = declaredField.getAnnotation(TableField.class).value();
            return tableFiled;
        } catch (Exception e) {
            return fieldName;
        }
    }

    @RequestMapping(value = "page", method = {RequestMethod.POST})
    @ApiOperation("分页查询")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public WtBaseResponse<WtPageBodyModel<T>> getPage(@RequestBody WtPageQuerySearchModel wtPageQueryModel) {
        this.volidate(wtPageQueryModel);

        QueryWrapper<T> condition = new QueryWrapper<T>();

        if (StringUtils.isNotBlank(wtPageQueryModel.getSearchKey()) && StringUtils.isNotBlank(wtPageQueryModel.getSearchValue())) {
            condition.like(wtPageQueryModel.getSearchKey(), wtPageQueryModel.getSearchValue());
        }

        IPage<T> page = baseService.page(new Page<T>(wtPageQueryModel.getPage(), wtPageQueryModel.getRows()), condition);
        return new WtBaseResponse(new WtPageBodyModel<T>(page.getTotal(), page.getCurrent(), page.getRecords()));
    }

    @ApiOperation("根据ID查询")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping(value = "get", method = {RequestMethod.POST})
    public WtBaseResponse<T> getById(@RequestBody WtPrimaryKeyReqeust<U> reqeust) {
        this.volidate(reqeust);
        return new WtBaseResponse(baseService.getById(reqeust.getId()));
    }

    @ApiOperation("通过id删除")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping(value = "del", method = {RequestMethod.POST})
    public WtBaseResponse<String> delete(@RequestBody WtPrimaryKeyReqeust<U> reqeust) {
        this.volidate(reqeust);
        baseService.removeById(reqeust.getId());
        return WtResponses.SUCCESS;
    }

    @ApiOperation("通过ID保存")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping(value = "save", method = {RequestMethod.POST})
    public WtBaseResponse<String> save(@RequestBody T t) {
        this.volidate(t);
        if (t == null) {
            throw WtExceptions.E_COMMON_KEY;
        }
        WtCommonPropertiesSetter.setDefaultCommonProperty(t, WtAuthContext.getUserId());
        baseService.saveOrUpdate(t);
        return WtResponses.SUCCESS;
    }
}
